<?php

namespace common\models\shop;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%shop_activity_product}}".
 *
 * @property int $id
 * @property int $activity_id 活动ID
 * @property int $group_id
 * @property string $name
 * @property string $description
 * @property string $thumb
 * @property string $price
 * @property string $discount
 * @property int $product_id 产品ID
 */
class ShopActivityProduct extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%shop_activity_product}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['activity_id', 'group_id', 'product_id'], 'integer'],
            [['name', 'thumb'], 'required'],
            [['price', 'discount'], 'number'],
            [['name'], 'string', 'max' => 50],
            [['description', 'thumb'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'activity_id' => '活动ID',
            'group_id' => 'Group ID',
            'name' => 'Name',
            'description' => 'Description',
            'thumb' => 'Thumb',
            'price' => 'Price',
            'discount' => 'Discount',
            'product_id' => '产品ID',
        ];
    }

    public function getActivity() {
        return $this->hasOne(ShopActivity::className(), ['id' => 'activity_id']);
    }

    public function getCover() {
        return Url::attach($this->thumb);
    }

    public function getWeblink() {
        return Url::weblink('shop.product', [
            'id' => (string) $this->product_id
        ]);
    }

    /**
     * @inheritdoc
     */
    public function fields() {
        return [
            'id',
            'name',
            'thumb' => 'cover',
            'description',
            'price' => function() {
                return number_format($this->price, 2);
            },
            'discount' => function() {
                return number_format($this->discount, 2);
            },
            'weblink'
        ];
    }

}
